from os import environ
from selenium import webdriver
from selenium.common.exceptions import ElementNotInteractableException,NoSuchElementException
from msedge.selenium_tools import EdgeOptions, Edge
from time import sleep

def get_edge(executable_path='D:/Programs/WebDriver/Edge/msedgedriver.exe', options=None):
    if options is None:
        options = EdgeOptions()
        options.add_argument('--log-level=3')
    return Edge(executable_path='D:/Programs/WebDriver/Edge/msedgedriver.exe', options=options)

def get_chrome(executable_path='D:/Programs/WebDriver/Edge/msedgedriver.exe', options=None):
    if options is None:
        options = webdriver.ChromeOptions()
        options.add_experimental_option('excludeSwitches', ['enable-logging'])
    return webdriver.Chrome(executable_path='D:/Programs/WebDriver/Edge/msedgedriver.exe', options=options)

get_browser = get_edge

def login(url='http://10.110.74.92/srun_portal_success?ac_id=9&theme=pro',
          username=None, password=None,
          timeout=2, driver=None):
    if username is None:
        username = environ['SRUN_USERNAME']
    if password is None:
        password = environ['SRUN_PASSWORD']
    if driver is None:
        driver = get_browser()
    driver.get(url=url)
    sleep(timeout)
    try:
        btn_login = driver.find_element_by_id('login-account')
        input_username = driver.find_element_by_id('username')
        input_username.send_keys(username)
        input_password = driver.find_element_by_id('password')
        input_password.send_keys(password)
        checkbox_remember = driver.find_element_by_id('remember')
        if not checkbox_remember.is_selected():
            checkbox_remember.click()
        btn_login.click()
        print('Login Sucessfully.')
    except NoSuchElementException as e:
        print(f'Login Button no found: probably already login.')
    except ElementNotInteractableException as e:
        print('element was not tractable')
    except Exception as e:
        print(f'unknown exception with type {type(e)}')


def logout(url='http://10.110.74.92/srun_portal_success?ac_id=9&theme=pro',
          timeout=2, driver=None):
    try:
        if driver is None:
            driver = get_browser()
        driver.get(url=url)
        sleep(timeout)
        btn_logout = driver.find_element_by_id('logout')
        if btn_logout.text == '注销':
            btn_logout.click()
            sleep(timeout)
            btn_confirm = driver.find_element_by_class_name('btn-confirm')
            if btn_confirm.text == '确认':
                btn_confirm.click()
                sleep(timeout)
                print('Logout Sucessfully.')
                return True
    except NoSuchElementException as e:
        print(f'Logout Button no found: probably not login.')
    except Exception as e:
        print(f'unknown exception with type {type(e)}')
    return False


def main(argv=None):
    if argv is None:
        import sys
        argv = sys.argv
    if len(argv) > 0:
        action = argv[1]
        if action == 'login':
            login()
        elif action == 'logout':
            logout()
        else:
            print(f'unknown action {action}')


if __name__ == '__main__':
    main()